package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuserDetailBean;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuserRvBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author : mundane
 * @time : 2017/9/27 18:36
 * @description :
 * @file : AuthorityDistributeAuserDetailPageFragment.java
 */

public class AuthorityDistributeAuserDetailPageFragment extends BaseFragment {

	private static final String KEY_PROCESS_ID = "key_process_id";
	private static final String KEY_TYPE = "key_type";
	private static final int PAGE_SIZE = 10;
	private List<AuserRvBean> mAllDataList;
	private int mFragmentListSize;
	private List<ArrayList<AuserRvBean>> mFragmentDataList;
	private List<Fragment> mFragmentList;

	private MyFragmentPagerAdapter mViewPagerAdapter;
	private NoHttpUtils.OnResponseListener mResponseListener = new NoHttpUtils.OnResponseListener() {
		@Override
		public void onSucceed(CommonClass commonClass, int what) {
			switch (what) {
				case 0:
					if (TextUtils.equals("0", commonClass.getCode())) {
						mStateLayout.showContentView();
						String data = commonClass.getData().toString();
						AuserDetailBean auserDetailBean = JSON.parseObject(data, AuserDetailBean.class);
						for (AuserDetailBean.ListBean listBean : auserDetailBean.list) {
							mAllDataList.add(new AuserRvBean(0, listBean.userName));
							for (AuserDetailBean.ListBean.FailDetail failDetail : listBean.failDetailDTOS) {
								mAllDataList.add(new AuserRvBean(1, failDetail.authName, failDetail.detailMsg));
							}
						}
						int remainder = mAllDataList.size() % PAGE_SIZE;
						if (remainder == 0) {
							mFragmentListSize = mAllDataList.size() / PAGE_SIZE;
						} else {
							mFragmentListSize = mAllDataList.size() / PAGE_SIZE + 1;
						}
						// 分页
						List<AuserRvBean> list = new ArrayList<>();
						for (int i = 0; i < mAllDataList.size(); i++) {
							list.add(mAllDataList.get(i));
							if (list.size() == 10 || i == mAllDataList.size() - 1) {
								ArrayList<AuserRvBean> perPageList = new ArrayList<>();
								for (AuserRvBean auserRvBean : list) {
									perPageList.add(auserRvBean);
								}
								mFragmentDataList.add(perPageList);
								list = new ArrayList<>();
							}
						}

						for (ArrayList<AuserRvBean> auserRvBeanArrayList : mFragmentDataList) {
							AuthorityAuserListFragment listFragment = AuthorityAuserListFragment.newInstance(auserRvBeanArrayList);
							mFragmentList.add(listFragment);
						}
						mViewPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragmentList);
						mCustomViewPagerInternal.setAdapter(mViewPagerAdapter);
						mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal);


					} else {
						ToastUtils.showToast(commonClass.getMsg());
					}
					break;
			}
		}

		@Override
		public void onFailed(String s, int i) {

		}

		@Override
		public void onFinish(int i) {

		}
	};

	public static AuthorityDistributeAuserDetailPageFragment newInstance(String processId, int type) {
		AuthorityDistributeAuserDetailPageFragment fragment = new AuthorityDistributeAuserDetailPageFragment();
		Bundle bundle = new Bundle();
		bundle.putString(KEY_PROCESS_ID, processId);
		bundle.putInt(KEY_TYPE, type);
		fragment.setArguments(bundle);
		return fragment;
	}

	private String mProcessId;
	private int type;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			mProcessId = bundle.getString(KEY_PROCESS_ID);
			type = bundle.getInt(KEY_TYPE);
		}
		mAllDataList = new ArrayList<>();
		mFragmentDataList = new ArrayList<>();
		mFragmentList = new ArrayList<>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_page_detail_distribute_authority, container, false);
		return view;
	}

	private CustomViewPagerInternal mCustomViewPagerInternal;
	private CirclePageIndicator mCirclePagerIndicator;
	private StateLayout mStateLayout;
	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mCustomViewPagerInternal = $(R.id.cvpi);
		mCirclePagerIndicator = $(R.id.cpi);
		mStateLayout = $(R.id.slt);
		mStateLayout.showProgressView(true);
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof AuthorityDistributeAuserDetailPageFragment) {
			mActivity.setTitle("权限分配失败详情");
		}
	}

	@Override
	public void requestDatas() {
		if (TextUtils.isEmpty(mProcessId)) {
			return;
		}
		Map<String, Object> map = new HashMap<>();
		map.put("page", 1);
		map.put("processId", mProcessId);
		map.put("type", type);
		NoHttpUtils.httpPost(UserCenterConstant.GET_AUTH_FAIL_DETAIL, map, mResponseListener, 0);
	}
}
